package com.zcfeng.ischool;

import android.app.Activity;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.util.Log;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;


public class MapActivity extends Activity implements  LocationSource, AMapLocationListener {

    MapView mMapView = null;
    AMap aMap=null;
    LocationSource.OnLocationChangedListener mListener;
    AMapLocationClient mlocationClient;
    AMapLocationClientOption mLocationOption;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        aMap.setLocationSource(this);
        aMap.setMyLocationEnabled(true);
        aMap.moveCamera(CameraUpdateFactory.zoomTo(16));
        aMap.addMarker(new MarkerOptions().position(new LatLng(33.92968,118.297284)).title("行政楼").snippet("领导办公")).showInfoWindow();
        MarkerOptions op= new MarkerOptions().position(new LatLng(33.932459,118.295348)).title("学院餐厅").snippet("一到6位于路西7位于路东").draggable(true).anchor(0.5f, 1);
        aMap.addMarker(op);
        aMap.addMarker(new MarkerOptions().position(new LatLng(33.926072,118.296872)).title("早签到2").snippet("").anchor(0.5f, 1));
        aMap.addMarker(new MarkerOptions().position(new LatLng(33.925772,118.292672)).title("早签到1").snippet("").anchor(0.5f, 1));
        aMap.addMarker(new MarkerOptions().position(new LatLng(33.932003,118.298131)).title("一号教学楼").snippet("新生报到处").anchor(0.5f, 1));
        aMap.addMarker(new MarkerOptions().position(new LatLng(33.93098,118.297752)).title("老图书馆").snippet("考研，软件研发，新生缴费").anchor(0.5f, 1));
        aMap.addMarker(new MarkerOptions().position(new LatLng(33.928062,118.29602)).title("现代教育中心").snippet("南部机房，北部语音室").anchor(0.5f, 1));
        aMap.addMarker(new MarkerOptions().position(new LatLng(33.932459,118.296348)).title("艺术楼").snippet("第七餐厅，和联通营业厅").anchor(0.5f, 1));
        aMap.addMarker(new MarkerOptions().position(new LatLng(33.926742,118.296272)).title("2,3,4号教学楼").snippet("自南向北分别为二三四").anchor(0.5f, 1));
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }
    @Override
    public void activate(OnLocationChangedListener listener) {
        mListener = listener;
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(this);
            mLocationOption = new AMapLocationClientOption();
            mlocationClient.setLocationListener(this);
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            mlocationClient.setLocationOption(mLocationOption);
            mlocationClient.startLocation();
        }
    }
    @Override
    public void deactivate() {

    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (mListener != null&&aMapLocation != null) {
            if (aMapLocation != null&&aMapLocation.getErrorCode() == 0) {
                mListener.onLocationChanged(aMapLocation);
            } else {
                String errText = "定位失败," + aMapLocation.getErrorCode()+ ": " + aMapLocation.getErrorInfo();
                Log.e("AmapErr",errText);
            }
        }
    }
    public void  back(View v){
        finish();
    }
}
